/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fy.report.manager;

import fy.config.FyConfig;
import fy.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Administrator
 */
public class ZcFyReportManager {

    private static org.apache.commons.logging.Log log = LogFactory.getLog(ZcFyReportManager.class);

    public static void operate() {
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
            DBUtil.setAutoCommit(conn, false);

            //更改已经被删除的跟踪事项
            pst = conn.prepareStatement("update formmain_3965 set field0014=0,field0005='',field0012='已删除',field0015=0 where id in (select f.id from formmain_3965 f left join CTP_ATTACHMENT t on f.FIELD0007 = t.SUB_REFERENCE left join col_summary s on s.id = t.GENESIS_ID where f.field0014 = 1 and s.id is null)");

            pst.execute();
            DBUtil.close(pst);

            conn.commit();

            pst = conn.prepareStatement("insert into FORM_RELATION_AUTHORITY (ID, MODULE_ID, MODULE_TYPE, USER_TYPE, USER_ID, AUTH_SOURCE_type) select round(9999999999999999999*dbms_random.value),s.id,1,0,?,0 from formmain_3965 f left join CTP_ATTACHMENT t on f.FIELD0007 = t.SUB_REFERENCE join COL_SUMMARY s on s.id = to_char(t.GENESIS_ID) where not exists (select * from FORM_RELATION_AUTHORITY a where a.MODULE_ID=s.id and a.USER_ID = ? )");

            pst.setLong(1, -1825607432309486470L);
            pst.setLong(2, -1825607432309486470L);
            pst.addBatch();

            pst.setLong(1, 630844575089446517L);
            pst.setLong(2, 630844575089446517L);
            pst.addBatch();

            pst.setLong(1, -8069906149773260922L);
            pst.setLong(2, -8069906149773260922L);
            pst.addBatch();

            pst.executeBatch();
            
            DBUtil.close(pst);

            //查找需要跟踪的事项
            pst = conn.prepareStatement("select f.id,t.GENESIS_ID,nvl(s.state,0) from formmain_3965 f left join CTP_ATTACHMENT t on f.FIELD0007 = t.SUB_REFERENCE left join col_summary s on t.GENESIS_ID = s.id where f.field0014 = 1");
            rs = pst.executeQuery();
            java.util.List<Object[]> stalkIDList = null;
            while (rs.next()) {
                if (stalkIDList == null) {
                    stalkIDList = new ArrayList<Object[]>();
                }
                stalkIDList.add(new Object[]{
                    rs.getLong(1), rs.getLong(2), rs.getInt(3)
                });
            }
            DBUtil.close(pst);
            int stalkIDLength = (stalkIDList != null ? stalkIDList.size() : 0);
            log.info(new StringBuilder().append("共查询到需要跟踪事项数:").append(stalkIDLength));
            for (int t = 0; t < stalkIDLength; t++) {
                Object[] stalkIDArray = stalkIDList.get(t);
                long summaryID = (Long) stalkIDArray[1];
//                //判断当前流程状态
//                pst = conn.prepareStatement("select nvl(state,0) from col_summary where id=?");
//                pst.setLong(1, summaryID);
//                rs = pst.executeQuery();
//                if (rs.next()) {
                int state = (Integer) stalkIDArray[2];
                if (state == 0) {
                    //流转中或者已撤销
//                   pst = conn.prepareStatement("select sum(decode(m.code,?,1,decode(m.code,?,1,0))),to_char(wmsys.wm_concat(m.name||'('||m.code||')')) from CTP_AFFAIR a left join ORG_MEMBER m on a.MEMBER_ID = m.id where a.OBJECT_ID = ? and a.STATE = 3 and a.IS_DELETE = 0");
                    //表单当前待办人员
                    pst = conn.prepareStatement("select to_char(wmsys.wm_concat(m.name||'('||m.code||')')) from CTP_AFFAIR a left join ORG_MEMBER m on a.MEMBER_ID = m.id where a.OBJECT_ID = ? and a.STATE = 3 and a.IS_DELETE = 0");
//                        pst.setString(1, "0022181");
//                        pst.setString(2, "068715");
                    pst.setLong(1, summaryID);
                    rs = pst.executeQuery();
                    boolean liuzhuang = false;
                    if (rs.next()) {
//                            int zongcai = rs.getInt(1);
                        String currentNode = rs.getString(1);
                        DBUtil.close(pst);
                        //不存在待办人员，判断流程已经撤销
                        liuzhuang = currentNode != null;
                        if (liuzhuang) {
                            //存在待办人员
                            pst = conn.prepareStatement("update formmain_3965 set field0014=1,field0015= decode((select a.id affairID from CTP_AFFAIR a where  a.OBJECT_ID = ? and a.STATE = 3 and a.IS_DELETE = 0 and a.MEMBER_ID in (630844575089446517) and rownum = 1),null,1,0) ,field0005=?,field0012=?,field0016 = (select decode(count(*),0,0,1) from CTP_AFFAIR a where a.MEMBER_ID = 630844575089446517 and a.STATE <> 3 and a.OBJECT_ID = ?) where id=?");
                          /*  for (int i = 0, length = stalkIDList.size(); i < length;
                                    i++) {*/
                                pst.setLong(1, summaryID);
                                pst.setString(2, currentNode);
                                pst.setString(3, "流程处理中");
                                pst.setLong(4, summaryID);
                                pst.setLong(5, (Long) stalkIDArray[0]);
                                pst.addBatch();
                           // }
                            pst.executeBatch();
                        }

                    }
                    if (!liuzhuang) {
                        //流程撤销处理
                        pst = conn.prepareStatement("update formmain_3965 set field0014=1,field0015=0,field0005='',field0012=?,field0016 =0 where id=?");
                       /* for (int i = 0, length = stalkIDList.size(); i < length;
                                i++) {*/
                            pst.setString(1, "撤销");
                            pst.setLong(2, (Long) stalkIDArray[0]);
                            pst.addBatch();
                        //}
                        pst.executeBatch();
                    }
                } else {
                    //流程结束或者终止
                    pst = conn.prepareStatement("update formmain_3965 set field0014=0,field0015=0,field0005='',field0012=?,field0016 = (select decode(count(*),0,0,1) from CTP_AFFAIR a where a.MEMBER_ID = 630844575089446517 and a.STATE <> 3 and a.OBJECT_ID = ?)  where id=?");
                 /*   for (int i = 0, length = stalkIDList.size(); i < length;
                            i++) {*/
                        pst.setString(1, state == 3 ? "结束" : (state == 1 ? "终止" : ""));
                        pst.setLong(2, summaryID);
                        pst.setLong(3, (Long) stalkIDArray[0]);
                        pst.addBatch();
                   // }
                    pst.executeBatch();
                }
                conn.commit();
//                }
            }
        } catch (Exception e) {
            DBUtil.rollback(conn);
//            e.printStackTrace();
            log.error("轮询，跟踪报表事项发生异常", e);
        } finally {
            DBUtil.setAutoCommit(conn, true);
            DBUtil.close(rs, pst, conn);
        }
    }
}
